package com.brsya.movie.widget;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.brsya.base.util.UiUtil;
import com.brsya.movie.R;


/**
 * Created by Brsya
 * CreateDate: 2022/6/4  18:36
 * Description:
 * 倍速
 */
public class PlaySpeedPopupWindow extends PopupWindow implements View.OnClickListener {
    private Context context;
    private View rootView;
    private TextView tvSpeed2;
    private TextView tvSpeed15;
    private TextView tvSpeed1;
    private TextView tvSpeed05;

    private OnSpeedChangeListener onSpeedChangeListener;
    private int popupHeight;
    private int popupWidth;

    public PlaySpeedPopupWindow(Context context) {
        super(context);
        this.context = context;
        init();
    }

    private void init() {
        rootView = LayoutInflater.from(context).inflate(R.layout.popup_play_speed_menu, null, false);
        setContentView(rootView);
        initView();

        rootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupHeight = rootView.getMeasuredHeight();
        popupWidth = rootView.getMeasuredWidth();

        setBackgroundDrawable(null);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(UiUtil.dpToPx(context, 80));
        // 设置外部点击取消
        setOutsideTouchable(true);
    }

    private void initView() {
        tvSpeed2 = rootView.findViewById(R.id.tv_speed_2);
        tvSpeed15 = rootView.findViewById(R.id.tv_speed_1_5);
        tvSpeed1 = rootView.findViewById(R.id.tv_speed_1);
        tvSpeed05 = rootView.findViewById(R.id.tv_speed_0_5);
        tvSpeed2.setOnClickListener(this);
        tvSpeed15.setOnClickListener(this);
        tvSpeed1.setOnClickListener(this);
        tvSpeed05.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.tv_speed_2) {
            if (onSpeedChangeListener != null) {
                onSpeedChangeListener.onSpeedChange(2f);
            }
        } else if (id == R.id.tv_speed_1_5) {
            if (onSpeedChangeListener != null) {
                onSpeedChangeListener.onSpeedChange(1.5f);
            }
        } else if (id == R.id.tv_speed_1) {
            if (onSpeedChangeListener != null) {
                onSpeedChangeListener.onSpeedChange(1f);
            }
        } else if (id == R.id.tv_speed_0_5) {
            if (onSpeedChangeListener != null) {
                onSpeedChangeListener.onSpeedChange(.5f);
            }
        }
    }
    /**
     * 设置显示在v上方(以v的左边距为开始位置)
     * @param v
     */
    public void showUp(View v) {
        //获取需要在其上方显示的控件的位置信息
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        //在控件上方显示
        showAtLocation(v, Gravity.NO_GRAVITY, (location[0]) - popupWidth / 2, location[1] - popupHeight);
    }

    /**
     * 设置显示在v上方（以v的中心位置为开始位置）
     * @param v
     */
    public void showUp2(View v) {
        //获取需要在其上方显示的控件的位置信息
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        //在控件上方显示
        showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
    }


    public void setOnSpeedChangeListener(OnSpeedChangeListener onSpeedChangeListener) {
        this.onSpeedChangeListener = onSpeedChangeListener;
    }

    public interface OnSpeedChangeListener{
        void onSpeedChange(float speed);
    }
}
